/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tools.gis.shapewriter;


import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryCollection;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;

import datatypes.Antenna;
import datatypes.RadiationPoint;
import java.io.File;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;

import modules.dimensioning.CoverageInterface;
import modules.dimensioning.solution.Solution;
import org.geotools.data.shapefile.shp.ShapeType;
import org.geotools.data.shapefile.shp.ShapefileWriter;
import org.geotools.geometry.jts.JTSFactoryFinder;




/**
 *
 * @author Pieter Backaert
 */
public class Write2Shapefile {

    private CoverageInterface ca ;

    public Write2Shapefile(CoverageInterface ci ){
        this.ca = ci;
    }

    public void writeSolutionObject2shapefile(Solution s, String filename){

        Antenna[] antennas = s.getAntennas();

        writeAntennas2shapefile(antennas, filename);

    }




    public void writeAntennas2shapefile(Antenna[] antennas, String filename){
    	writeAntennas2shapefile(antennas, filename, true);
    	writeAntennas2shapefile(antennas, filename, false);
    }

    private void writeAntennas2shapefile(Antenna[] antennas, String filename, boolean printActive){
    	filename += (printActive ? "Active" : "Inactive");
        File shpAL = new File(filename+".ant.shp");
        File shxAL = new File(filename+".ant.shx");
        File shpAC = new File(filename+".rad.shp");
        File shxAC = new File(filename+".rad.shx");

        

        FileChannel fcShpAL = null;
        FileChannel fcShxAL = null;
        FileChannel fcShpAC = null;
        FileChannel fcShxAC = null;
        try {
             //   System.out.println( " .getChannel()"  );
             fcShpAL = new FileOutputStream( shpAL ).getChannel();
             fcShxAL = new FileOutputStream( shxAL ).getChannel();
             fcShpAC = new FileOutputStream( shpAC ).getChannel();
             fcShxAC = new FileOutputStream( shxAC ).getChannel();


            // System.out.println( " new ShapefileWriter( fcShp ,fcShx ); " );
             ShapefileWriter writerAL = new ShapefileWriter( fcShpAL ,fcShxAL ); //shp.getChannel(),shx.getChannel() );
             ShapefileWriter writerAC = new ShapefileWriter( fcShpAC ,fcShxAC );

             //System.out.println( "Geometry[] g = new Geometry[antennas.length];"  );
             Geometry[] gPoints = new Geometry[antennas.length];
             Geometry[] gCircles = new Geometry[antennas.length];
             int index=0;

            //GeometryCollection geoms;
            for(int i=0 ; i<antennas.length ; i++){
                if( antennas[i].getActive() == printActive){
                    gPoints[index] = (Geometry) antennas[i].getSimpleFeature().getDefaultGeometry();
                    //gCircles[i] = createCircle( antennas[i].getX() , antennas[i].getY() , antennas[i].getRange() );
                   if(printActive) {
                    gCircles[index] = ca.getCoverage( antennas[i]);
                   } else {
                	gCircles[index] = tools.gis.GisTools.createCircleInLambertFormat( antennas[i].getX() , antennas[i].getY() , 1, 32);
                   }
                   
                    index++;
           //         System.out.println( "antennas[i].getX()=" + antennas[i].getX() + " --- " + "antennas[i].getY()=" + antennas[i].getY() + " --- " +
            //                                " Range()=" +  ca.getRangeRadius(antennas[i]) );
                }
            }

             Geometry[] gPointsNew = new Geometry[index];
             Geometry[] gCirclesNew = new Geometry[index];
             for(int i=0 ; i<index ; i++){
                 gPointsNew[i] = gPoints[i];
                 gCirclesNew[i] = gCircles[i];
             }
             // nu zitten er geen null-beesten in de arrays


             GeometryCollection gcP = new GeometryCollection(gPointsNew, JTSFactoryFinder.getGeometryFactory( null ) );
             GeometryCollection gcC = new GeometryCollection(gCirclesNew, JTSFactoryFinder.getGeometryFactory( null ) );
             //System.out.println("GeometryCollection gc = new GeometryCollection(g, JTSFactoryFinder.getGeometryFactory( null ) );" );

             writerAL.write(gcP, ShapeType.POINT);
             writerAC.write(gcC, ShapeType.POLYGON);
             //System.out.println("writer.write(gc, ShapeType.POINT);" );

             writerAL.close();
             writerAC.close();
             //System.out.println("writer.close();" );


        } catch(Exception e){
            System.out.println( " error, tof !"         );
        }
    }

    public static void writeShapes(Geometry[] g, String filename){
        File shpAL = new File(filename+".shp");
        File shxAL = new File(filename+".shx");

        FileChannel fcShpAL = null;
        FileChannel fcShxAL = null;
        try {
             //   System.out.println( " .getChannel()"  );
             fcShpAL = new FileOutputStream( shpAL ).getChannel();
             fcShxAL = new FileOutputStream( shxAL ).getChannel();


            // System.out.println( " new ShapefileWriter( fcShp ,fcShx ); " );
             ShapefileWriter writerAL = new ShapefileWriter( fcShpAL ,fcShxAL ); //shp.getChannel(),shx.getChannel() );

             GeometryCollection gcP = new GeometryCollection(g, JTSFactoryFinder.getGeometryFactory( null ) );
             //System.out.println("GeometryCollection gc = new GeometryCollection(g, JTSFactoryFinder.getGeometryFactory( null ) );" );

             writerAL.write(gcP, ShapeType.POLYGON);
             //System.out.println("writer.write(gc, ShapeType.POINT);" );

             writerAL.close();
             //System.out.println("writer.close();" );


        } catch(Exception e){
            System.out.println( " error, tof !"         );
        }
    }




    public void writeRadiationPoints2shapefile(RadiationPoint[] rp, String filename){
    	filename +=  "radiation_points";
        File shpAL = new File(filename+".radiationPts.shp");
        File shxAL = new File(filename+".radiationPts.shx");

        FileChannel fcShpAL = null;
        FileChannel fcShxAL = null;

        try {

             fcShpAL = new FileOutputStream( shpAL ).getChannel();
             fcShxAL = new FileOutputStream( shxAL ).getChannel();


             ShapefileWriter writerAL = new ShapefileWriter( fcShpAL ,fcShxAL );

             Geometry[] gPoints = new Geometry[rp.length];
        

            //GeometryCollection geoms;
            for(int i=0 ; i<rp.length ; i++){
                    double xCo = rp[i].getXCo();
                    double yCo = rp[i].getYCo();

                    GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory( null );
                    Coordinate coord = new Coordinate( xCo, yCo );
                    Point point = geometryFactory.createPoint( coord );

                    gPoints[i] = (Geometry) point;
 
            }
             GeometryCollection gcP = new GeometryCollection(gPoints, JTSFactoryFinder.getGeometryFactory( null ) );
             writerAL.write(gcP, ShapeType.POINT);
         
             writerAL.close();
         
        } catch(Exception e){
            System.out.println( " error!" );
        }
    }

 }


